<template>
  <div class="chat-input-container">
    <div class="chat-input">
      <div class="input-area"
           contenteditable="true"
           ref="messageInput"
           @input="updateMessage"
           @keyup.enter="sendMessage"
           :placeholder="isSending ? '正在发送...' : '输入消息...'"></div>
      <div class="send-icon" :class="isSending?'disabled':''" @click="sendMessage" :disabled="isSending">
        <el-icon><Promotion /></el-icon>
      </div>
    </div>
  </div>
</template>

<script>
import { Promotion } from "@element-plus/icons-vue";
import { useChatStore } from '@/stores/chat';

export default {
  components: {
    Promotion,
  },
  data() {
    return {
      message: '',
    };
  },
  computed: {
    isSending() {
      return useChatStore().isSending;
    },
  },
  methods: {
    updateMessage(event) {
      this.message = event.target.innerText;
    },
    sendMessage() {
      if (this.message.trim() && !this.isSending) {
        this.$emit('send', this.message);
        this.message = '';
        this.$refs.messageInput.innerText = '';
      }
    },
  },
};
</script>

<style scoped>
.chat-input-container {
  padding: 0 20px 10px; /* 底部内边距减少 */
}

.chat-input {
  display: flex;
  align-items: center;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto; 
  padding: 8px 8px 8px 16px; /* 增加左侧内边距 */
  border-radius: 24px;
  border: 1px solid #ddd;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
  background: white;
  position: relative; /* 添加相对定位 */
}

.input-area {
  flex: 1;
  padding: 8px 50px 8px 0px; /* 右侧留出按钮空间 */
  font-size: 16px;
  line-height: 1.5;
  min-height: 38px;
  max-height: 120px;
  overflow-y: auto; /* 保留输入框滚动条 */
  white-space: pre-wrap;
  word-wrap: break-word;
  outline: none;
  border-radius: 0; /* 移除圆角 */
  background-color: transparent; /* 透明背景 */
}

.input-area[contenteditable]:empty:before {
  content: attr(placeholder);
  color: #aaa;
}

.send-icon {
  position: absolute; /* 绝对定位在父容器内 */
  right: 10px; /* 调整右侧位置 */
  top: 50%; /* 垂直居中 */
  transform: translateY(-50%); /* 精确垂直居中 */
  cursor: pointer;
  font-size: 22px;
  padding: 8px;
  background-color: #409EFF;
  border-radius: 50%;
  color: #fff;
  transition: background-color 0.3s ease;
}

.send-icon:hover {
  background-color: #66b1ff;
}

.send-icon.disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

@media (max-width: 1024px) {
  .chat-input-container {
    padding: 0 15px 8px;
  }
  
  .chat-input {
    padding: 7px 7px 7px 12px;
    max-width: 800px;
  }
  
  .input-area {
    padding: 7px 45px 7px 0;
    min-height: 36px;
  }
  
  .send-icon {
    right: 8px;
    font-size: 20px;
    padding: 7px;
  }
}

@media (max-width: 768px) {
  .chat-input-container {
    padding: 0 10px 5px;
  }
  
  .input-area {
    padding: 6px 40px 6px 0;
    font-size: 14px;
    min-height: 34px;
    overflow-y: auto; /* 保留输入框滚动条 */
  }
  
  .send-icon {
    right: 7px;
    font-size: 18px;
    padding: 6px;
  }
}
</style>